package com.haochang.chunkvod.app.utils;

import com.haochang.chunkvod.R;

import android.content.Context;
import android.media.AudioManager;
import android.media.SoundPool;
import android.support.annotation.NonNull;

/**
 * SoundPool播放短声音工具
 *     @author Yangjie
 *     className SoundPoolUtil
 *     created at  2016/11/21  17:04
 */
public class SoundPoolUtil
{

	private static volatile SoundPoolUtil singleton;
	private SoundPool soundPool;//SoundPool实例
	private static boolean isPlaying;

	private SoundPoolUtil()
	{
	}

	public static SoundPoolUtil getInstance()
	{
		if (singleton == null)
		{
			synchronized (SoundPoolUtil.class)
			{
				if (singleton == null)
				{
					singleton = new SoundPoolUtil();
				}
			}
		}
		return singleton;
	}

	/**
	 * 开始播放短音效
	 * @param context 上下文
	 */
	public void startPlay(@NonNull Context context)
	{
		if (soundPool == null)
		{
			soundPool = new SoundPool(3, AudioManager.STREAM_SYSTEM, 5);
			soundPool.load(context, R.raw.tip, 1);
		}
		if (!isPlaying)
		{
			soundPool.play(1, 1, 1, 0, -1, 1);//-1表示循环播放
			isPlaying = true;
		}
	}

	/**
	 * 停止播放短音频
	 */
	public void stopPlay()
	{
		if (null != soundPool)
		{
			soundPool.pause(1);//streamId,这个是音频的id,上面传的是1
			soundPool.release();
			soundPool = null;
		}
		isPlaying=false;
	}
}